package com.feng.zshop.front.controller;

import com.feng.zshop.backend.param.ProductParam;
import com.feng.zshop.backend.service.ProductService;
import com.feng.zshop.backend.service.ProductTypeService;
import com.feng.zshop.common.constant.PaginationConstant;
import com.feng.zshop.model.Product;
import com.feng.zshop.model.ProductType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.OutputStream;
import java.util.List;

/**
 * @author Lee
 * @date 2019/2/26
 */
@Controller
@RequestMapping("/front/product")
public class ProductController {

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private ProductService productService;

    @RequestMapping("/search")
    public String search(@RequestParam(value = "pageNum", defaultValue = PaginationConstant.PAGE_NUMBER_STRING) Integer pageNum, ProductParam productParam, Model model){
        PageHelper.startPage(pageNum, PaginationConstant.FRONT_PAGE_SIZE);
        List<Product> products = productService.getByParam(productParam);
        OutputStream outputStream = null;
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        model.addAttribute("pageInfo",pageInfo);
        return "main";
    }

    @ModelAttribute("productTypes")
    public List<ProductType> loadProductType(Model model){
        return productTypeService.findProductTypeEnable();
    }

}
